package com.cxg.fairy.ec.main.personal.needs;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ScrollView;

import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/8.
 */

public class NewGoodsNeedsDelegate extends LatteDelegate implements ISuccess {

    @BindView(R2.id.scrollView_Needs)
    ScrollView mNeedsScrollView = null;

    @BindView(R2.id.met_supplier_name)
    MaterialEditText mNeedsSupplierName = null;

    @BindView(R2.id.met_goods_refer_price)
    MaterialEditText mNeedsGoodsReferPrice = null;

    @BindView(R2.id.met_new_needs_remark)
    MaterialEditText mNeedsRemark = null;

    @BindView(R2.id.met_new_needs_tel)
    MaterialEditText mNeedsTel = null;

    @BindView(R2.id.ms_new_needs_types)
    MaterialSpinner mNeedsTypes = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_new_goods_needs;
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final Window w = this.getSupportDelegate().getActivity().getWindow();
        scrollUp(mNeedsSupplierName,w,3);
        scrollUp(mNeedsGoodsReferPrice,w,2.5);
        scrollUp(mNeedsRemark,w,2);
        scrollUp(mNeedsTel,w,1);

        RestClient.builder()
                .url("/api/favor_list.php")
                .loader(getContext())
                .success(this)
                .build()
                .get();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSuccess(String response) {
        //商品需求分类
        mNeedsTypes.setItems(getResources().getStringArray(R.array.new_needs_types));
        mNeedsTypes.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
            }
        });
    }


    public void scrollUp(MaterialEditText met,final Window w,final double n) {
        met.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                DisplayMetrics outMetrics = new DisplayMetrics();
                w.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
                int wHeight = outMetrics.heightPixels;
                int vHeight = mNeedsScrollView.getChildAt(0).getMeasuredHeight();
                mNeedsScrollView.scrollTo(0,(int)((vHeight-wHeight)/n));
                return false;
            }
        });
    }

}
